<?php

namespace App\Admin\Controllers;

use App\Models\Order;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class OrderController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }
    public function userOrder()
    {
        return Admin::content(function (Content $content) {

            $content->header('单一用户的所有订单');
            $content->description('');
            $content->body($this->gridUser());
        });
    }
    protected function gridUser()
    {
        return Admin::grid(Order::class, function (Grid $grid) {
            $grid->model()->where('user_id',request('user_id'));
            $grid->id('ID')->sortable();
            $grid->column('order_number','订单编号');
            $grid->order_type('订单类型')->display(function ($type){
                switch ($type){
                    case '1':
                        return '预定';
                        break;
                    case '2':
                        return '外卖扫码';
                        break;
                    case '3':
                        return '预定扫码';
                        break;
                    case '4':
                        return '直接付款';
                        break;
                    case '5':
                        return '充值';
                        break;
                    case '0':
                        return '外卖';
                        break;
                    default:
                        return 'yes';
                        break;
                }
            });
            $grid->column('order_price','订单总额');
            $grid->column('order_amount','订单欠额')->display(function ($amount){
                return isset($amount) ? $amount:'无';
            });
            $grid->column('created_at','创建时间');
            $grid->column('updated_at','更新时间');
        });
    }
    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Order::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Order::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
    public function orders(){
        return Admin::content(function (Content $content) {

            $content->header('用户所属订单列表');
            $content->description('');

            $content->body($this->gridOrders());
        });
    }
    protected function gridOrders()
    {
//        $mid = request('user_id');
        return Admin::grid(Order::class, function (Grid $grid) {

            $grid->model()->where('user_id',request('user_id'));
            $grid->id('ID')->sortable();
//            $grid->column('name', '用户名');
//            $grid->column('money', '用户金额');
//            $grid->column('userInfoSys.real_name','真实姓名')->display(function ($name){
//                return isset($name) ? $name:'无';
//            });
//            $grid->column('userInfoSys.phone','联系电话')->display(function ($phone){
//                return isset($phone) ? $phone:'无';
//            });
//            $grid->column('userInfoSys.id_card','身份证')->display(function ($card){
//                return isset($card) ? $card:'无';
//            });
            $grid->column('created_at', '创建时间');
            $grid->disableCreateButton();



        });
    }
}
